<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16/016
 * Time: 21:00
 */

namespace app\api\controller;


use app\common\model\MemberConfig;
use think\Db;
use think\Exception;

class Member extends User
{

    protected $checkMember = false;


    /**
     * 会员显示界面
     */
    public function memberView()
    {

        $config = self::getExplain('member_img,clothes_rebate,year_member_img,year_clothes_rebate');

        $rows   = MemberConfig::order('sort')->field('id,end_time,price,type,ios_id')->select();

        api_return(1,'获取成功',[
            'userInfo' => $this->getMember(),
            'config' => $config,
            'rows' => $rows
        ]);
    }





    /**
     * 购买会员
     */
    public function buyMember(int $id)
    {
        $userInfo = $this->getMember();

        $config = MemberConfig::get($id);

        Db::startTrans();
        try{

            $this->moneyDec($config['price']);

            $update = self::getMemberInfo($userInfo,$config);

            Db::name('user_extend')->where('user_id',$this->user_id)->update($update);

            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            api_return(0,$e->getMessage());
        }

        api_return(1,'购买成功');

    }


}